//
//  GMComboDetailViewController.m
//  GuardMan
//
//  Created by steven on 2017/3/25.
//  Copyright © 2017年 steven. All rights reserved.
//

#import "GMComboDetailViewController.h"
#import "GMOrderDataController.h"

@interface GMComboDetailViewController ()

@property (nonatomic, strong)GMOrderDataController *orderDataController;

@end

@implementation GMComboDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.orderDataController = [[GMOrderDataController alloc] init];
    [self setupMainView];
    [self renderSuccessView];
}

- (void)setupMainView {
    [self.view addSubview:self.contentBgView];
    [self.contentBgView addSubview:self.iconImageView];
    [self.contentBgView addSubview:self.separatorLineView];
    [self.contentBgView addSubview:self.countTitleLabel];
    [self.contentBgView addSubview:self.countLabel];
    [self.contentBgView addSubview:self.detailLabel];
    [self.contentBgView addSubview:self.priceLabel];
    [self.contentBgView addSubview:self.scoreLabel];
    [self.view addSubview:self.buyButton];
    
    // content background view
    [self.contentBgView autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.contentBgView autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    [self.contentBgView autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
    [self.contentBgView autoSetDimension:ALDimensionHeight toSize:168];
    
    // icon image view
    [self.iconImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:32];
    [self.iconImageView autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:28];
    [self.iconImageView autoSetDimensionsToSize:CGSizeMake(34, 56)];
    
    // separator line view
    [self.separatorLineView autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.iconImageView];
    [self.separatorLineView autoSetDimensionsToSize:CGSizeMake(1, 44)];
    [self.separatorLineView autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:self.iconImageView withOffset:16];
    
    // count title label
    [self.countTitleLabel autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:self.separatorLineView withOffset:8];
    [self.countTitleLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.separatorLineView withOffset:2];
    
    // count label
    [self.countLabel autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:self.countTitleLabel withOffset:6];
    [self.countLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.countTitleLabel];
    
    // detail label
    [self.detailLabel autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:self.separatorLineView withOffset:8];
    [self.detailLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.separatorLineView withOffset:-2];
    
    // price label
    [self.priceLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:14];
    [self.priceLabel autoAlignAxis:ALAxisVertical toSameAxisOfView:self.contentBgView withOffset:-4];
    
    // score label
    [self.scoreLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.priceLabel];
    [self.scoreLabel autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:self.priceLabel withOffset:2];
    
    // buy button label
    [self.buyButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:62];
    [self.buyButton autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:18];
    [self.buyButton autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:18];
    [self.buyButton autoSetDimension:ALDimensionHeight toSize:54];
}

- (void)renderSuccessView {
    self.countLabel.text = [NSString stringWithFormat:@"%@", self.comboModel.quantity];
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@", self.comboModel.price];
    self.detailLabel.text = @"本套餐包含5个守卫，每个守卫都可用于守卫用户或者车辆";
    self.scoreLabel.text = @"购买可或得20积分";
    [self.buyButton setTitle:[NSString stringWithFormat:@"确认支付 ￥%@", self.comboModel.price] forState:UIControlStateNormal];
}

- (void)buyButtonPrssed {
    NSDictionary *params = @{@"comboId": [NSString stringWithFormat:@"%ld", (long)self.comboModel.id], @"payType":@"apple"};
    [self.orderDataController buyComboWithParams:params callback:^(NSString *error) {
        if (error == nil) {
            [self renderBuySuccessView];
        } else {
            [self showAlertMessage:error];
        }
    }];
}

- (void)renderBuySuccessView {
    [self showAlertMessage:@"购买成功"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UIView *)contentBgView {
    if (!_contentBgView) {
        _contentBgView = [[UIView alloc] init];
        _contentBgView.backgroundColor = [UIColor whiteColor];
    }
    
    return _contentBgView;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.image = [UIImage imageNamed:@"guard-icon"];
    }
    
    return _iconImageView;
}

- (UIView *)separatorLineView {
    if (!_separatorLineView) {
        _separatorLineView = [[UIView alloc] init];
        _separatorLineView.backgroundColor = [UIColor grayColor];
    }
    
    return _separatorLineView;
}

- (UILabel *)countTitleLabel {
    if (!_countTitleLabel) {
        _countTitleLabel = [[UILabel alloc] init];
        _countTitleLabel.text = @"守卫侠个数";
        _countTitleLabel.font = [UIFont systemFontOfSize:14];
    }
    
    return _countTitleLabel;
}

- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.font = [UIFont systemFontOfSize:22];
        _countLabel.textColor = BUTTON_ACTIVE_COLOR;
    }
    
    return _countLabel;
}

- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:8];
    }
    
    return _detailLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont systemFontOfSize:36];
    }
    
    return _priceLabel;
}

- (UILabel *)scoreLabel {
    if (!_scoreLabel) {
        _scoreLabel.font = [UIFont systemFontOfSize:10];
        _scoreLabel.textColor = [UIColor grayColor];
    }
    
    return _scoreLabel;
}

- (GMButton *)buyButton {
    if (!_buyButton) {
        _buyButton = [[GMButton alloc] init];
        [_buyButton addTarget:self action:@selector(buyButtonPrssed) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _buyButton;
}

@end
